package com.example.limit.controller;

import com.example.limit.entity.PurchaseRule;
import com.example.limit.mapper.PurchaseRuleMapper;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/rules")
public class PurchaseRuleController {
    
    private final PurchaseRuleMapper ruleMapper;
    
    public PurchaseRuleController(PurchaseRuleMapper ruleMapper) {
        this.ruleMapper = ruleMapper;
    }
    
    @PostMapping
    public String createRule(@RequestBody PurchaseRule rule) {
        ruleMapper.insert(rule);
        return "规则创建成功";
    }
    
    @PutMapping("/{id}")
    public String updateRule(@PathVariable Long id, @RequestBody PurchaseRule rule) {
        rule.setId(id);
        ruleMapper.update(rule);
        return "规则更新成功";
    }
    
    @DeleteMapping("/{id}")
    public String deleteRule(@PathVariable Long id) {
        ruleMapper.delete(id);
        return "规则删除成功";
    }
    
    @GetMapping("/{id}")
    public PurchaseRule getRule(@PathVariable Long id) {
        return ruleMapper.selectById(id);
    }
    
    @GetMapping
    public List<PurchaseRule> getActiveRules() {
        return ruleMapper.selectActiveRules();
    }
    
    @GetMapping("/type/{ruleType}")
    public List<PurchaseRule> getRulesByType(@PathVariable Integer ruleType) {
        return ruleMapper.selectByRuleType(ruleType);
    }
}